﻿' ****************************************************************************
' Copyright (C) 2009 Tony George
' 
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
' 
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
' 
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'
' ****************************************************************************

Public Class frmShortcut
    Private menuAdded As Boolean = False
    Private Sub frmShortcut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If SelectedTask Is Nothing Then
            Me.optDesktop.Checked = True
            Me.optUsersAll.Checked = True
            Me.txtArgs.Text = ""
            Me.txtDescription.Text = ""
            Me.txtFolder.Text = ""
            Me.txtIconFile.Text = ""
            Me.txtIconIndex.Text = ""
            Me.txtName.Text = ""
            Me.txtSource.Text = "%%T\"
            Me.txtWorkDir.Text = ""
        Else
            Dim s As String = SelectedTask.Text
            s = s.Replace("Shortcut=" & """", "")
            s = s.Remove(s.Length - 1, 1)
            Dim arr() As String = Split(s, ",")
            Select Case arr(0).Replace("u", "")
                Case "P"
                    Me.optPrograms.Checked = True
                Case "D"
                    Me.optDesktop.Checked = True
                Case "S"
                    Me.optStartMenu.Checked = True
                Case "T"
                    Me.optStartup.Checked = True
            End Select
            If arr(0).Contains("u") Then
                Me.optUsersAll.Checked = True
            Else
                Me.optUsersCurrent.Checked = True
            End If
            Me.txtSource.Text = arr(1).Replace("{", "").Replace("}", "").Replace("\\", "\").Replace("\""", """")
            Me.txtArgs.Text = arr(2).Replace("{", "").Replace("}", "")
            Me.txtFolder.Text = arr(3).Replace("{", "").Replace("}", "")
            Me.txtDescription.Text = arr(4).Replace("{", "").Replace("}", "")
            Me.txtName.Text = arr(5).Replace("{", "").Replace("}", "")
            Me.txtWorkDir.Text = arr(6).Replace("{", "").Replace("}", "")
            Me.txtIconFile.Text = arr(7).Replace("{", "").Replace("}", "")
            Me.txtIconIndex.Text = arr(8).Replace("{", "").Replace("}", "")
        End If

        If Not menuAdded Then
            addMenuItems(Me.cmVar, Me.txtSource)
            menuAdded = True
        End If
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        If Me.txtSource.Text.Trim = "" Or Me.txtSource.Text.Trim = "%%T\" Then
            MsgBox(GetLangString("2116"))
            Exit Sub
        End If

        Dim s As String = ""
        s += "Shortcut="
        s += """"
        If Me.optDesktop.Checked Then
            s += "D"
        ElseIf Me.optStartMenu.Checked Then
            s += "S"
        ElseIf Me.optPrograms.Checked Then
            s += "P"
        ElseIf Me.optStartup.Checked Then
            s += "T"
        End If
        If Me.optUsersAll.Checked Then
            s += "u"
        End If
        s += (",{" & Me.txtSource.Text & "}").Replace("\", "\\").Replace("""", "\""")
        s += ",{" & Me.txtArgs.Text & "}"
        s += ",{" & Me.txtFolder.Text & "}"
        s += ",{" & Me.txtDescription.Text & "}"
        s += ",{" & Me.txtName.Text & "}"
        s += ",{" & Me.txtWorkDir.Text & "}"
        s += ",{" & Me.txtIconFile.Text & "}"
        s += ",{" & Me.txtIconIndex.Text & "}"
        s += """"

        If SelectedTask Is Nothing Then
            frmMain.lvTasks.Items.Add(s).Checked = True
        Else
            SelectedTask.Text = s
        End If

        Me.Close()
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.Close()
    End Sub

    Private Sub cmVar_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cmVar.Opening
        Dim cm As ContextMenuStrip = DirectCast(sender, ContextMenuStrip)
        ParentTextBox = cm.SourceControl
    End Sub
End Class